/**
 * File Name   : DiscountTypeAction.java
 * Description : 
 */
package com.hh.admin.discounttype.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.admin.discounttype.form.DiscountTypeBean;
import com.hh.admin.discounttype.form.DiscountTypeForm;
import com.hh.admin.discounttype.service.DiscountTypeService;
import com.hh.common.consts.AdminConst;

/**
 * 消费打折类别管理
 * @author 冯昌硕
 * @create 2013-3-10 上午12:45:38
 */
public class DiscountTypeAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(DiscountTypeAction.class);
    
    private CommonService commonService;
    
    private DiscountTypeService discounttypeService;
    
    /**
     * 后台界面查询消费打折信息列表
     * @create 2013-3-10 上午12:58:47
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        DiscountTypeForm dtForm = (DiscountTypeForm) form;
        
        // 查询当前页要显示的记录
        DiscountTypeBean paramBean = new DiscountTypeBean();
        List<DiscountTypeBean> discounttypeLst = 
                commonService.selRecordList(dtForm, "discounttype.selDiscountTypeList", paramBean);
        request.setAttribute("discounttypeLst", discounttypeLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "discounttype.selDiscountTypeCount");
        dtForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / dtForm.getPageSize();
        if(totleCnt % dtForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        dtForm.setTotlePage(pageCnt);
        log.debug("查询消费打折类型记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 添加消费打折类型记录
     * @create 2013-3-10 上午9:03:18
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountTypeForm dtForm = (DiscountTypeForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        dtForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = discounttypeService.insertDiscounttype(dtForm);
        log.debug("新增消费打折类型ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 跳转到内容详情编辑页面
     * @create 2013-3-10 上午9:16:09
     */
    public ActionForward detail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountTypeForm dtForm = (DiscountTypeForm) form;
        int id = dtForm.getId();
        
        DiscountTypeBean resultBean = discounttypeService.getDiscountTypeDetail(id);
        request.setAttribute("discounttypeDetail", resultBean);
        
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的内容信息保存
     * @create 2013-3-10 上午9:30:50
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountTypeForm dtForm = (DiscountTypeForm) form;
        int updCnt = discounttypeService.updateDiscountType(dtForm);
        log.debug("修改消费打折类型[" + dtForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 删除单条记录
     * @create 2013-3-10 上午9:46:16
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountTypeForm dtForm = (DiscountTypeForm) form;
        
        int id = dtForm.getId();
        log.debug("消费打折类型删除，要删除的产品类型ID=[" + id + "]");
        int delCnt = discounttypeService.deleteOne(id);
        log.debug("消费打折类型[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2013-3-10 上午9:49:47
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DiscountTypeForm dtForm = (DiscountTypeForm) form;
        Integer[] ids = dtForm.getIds();
        
        int delCnt = discounttypeService.deleteMany(ids);
        log.debug("批量消费打折类型删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }

    public void setDiscounttypeService(DiscountTypeService discounttypeService) {
        this.discounttypeService = discounttypeService;
    }
    
}
